package com.jinmdz.fmis.dao.admin.dao.provider;

import com.jinmdz.fmis.dao.base.BaseProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.ArrayList;

/**
 * 系统平台Provider
 *
 * @author LiCongLu
 * @date 2020-03-20 11:02
 */
public class AdminProvider extends BaseProvider {

    /**
     * 按照系统编码获取系统主键
     *
     * @param systemCode 系统编码
     * @param userId     账号主键
     * @return
     * @author LiCongLu
     * @date 2020-03-24 16:59
     */
    public String getSystemIdBySystemCode(@Param("systemCode") String systemCode, @Param("userId") Integer userId) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" business_system.id ");
                SELECT(builder.toString());
                FROM(" business_system ");
                JOIN(" business_system_role on business_system_role.business_system_id = business_system.id ");
                JOIN(" role_user_relation on role_user_relation.business_system_role_id = business_system_role.id ");
                WHERE(" business_system.deleted = 0 and business_system_role.deleted = 0 and role_user_relation.deleted = 0 ");
                WHERE(" business_system.disable_state = 0 and business_system_role.disable_state = 0 ");
                WHERE(" business_system.system_code = #{systemCode} and role_user_relation.system_user_id = #{userId} ");
                ORDER_BY(" business_system.id limit 1 ");
            }
        }.toString();
    }

    /**
     * 按照帐号主键查询菜单列表
     *
     * @param systemId 系统主键
     * @param userId   帐号主键
     * @return
     * @author LiCongLu
     * @date 2020-03-20 10:58
     */
    public String listAdminMenuListByUserId(@Param("systemId") Integer systemId, @Param("userId") Integer userId) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" distinct business_system_menu.id,business_system_menu.parent_id,menu_name,menu_route,menu_redirect_route,menu_icon,sort ");
                SELECT(builder.toString());
                FROM(" business_system_menu ");
                JOIN(" role_menu_relation on role_menu_relation.business_system_menu_id = business_system_menu.id ");
                JOIN(" role_user_relation on role_user_relation.business_system_role_id = role_menu_relation.business_system_role_id ");
                WHERE(" business_system_menu.deleted = 0 and role_menu_relation.deleted = 0 and role_user_relation.deleted = 0 ");
                WHERE(" business_system_menu.disable_state = 0 ");
                WHERE(" business_system_id = #{systemId} and role_user_relation.system_user_id = #{userId} ");
                ORDER_BY(" parent_id,sort ");
            }
        }.toString();
    }

    /**
     * 按照帐号主键查询菜单按钮列表
     *
     * @param systemId 系统主键
     * @param userId   帐号主键
     * @return
     * @author LiCongLu
     * @date 2020-03-20 11:18
     */
    public String listAdminMenuButtonListByUserId(@Param("systemId") Integer systemId, @Param("userId") Integer userId) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" distinct business_system_menu_button.id,business_system_menu.id as menu_id,menu_route,button_name,button_describe ");
                builder.append(" ,( select count(*) from role_menu_button_relation ");
                builder.append("    where deleted = 0 ");
                builder.append("     and business_system_role_id = role_user_relation.business_system_role_id ");
                builder.append("     and business_system_menu_id = business_system_menu.id ");
                builder.append("     and business_system_menu_button_id = business_system_menu_button.id ");
                builder.append("   ) as selected ");
                SELECT(builder.toString());
                FROM(" business_system_menu_button ");
                JOIN(" business_system_menu on business_system_menu.id = business_system_menu_button.business_system_menu_id ");
                JOIN(" role_menu_relation on role_menu_relation.business_system_menu_id = business_system_menu.id ");
                JOIN(" role_user_relation on role_user_relation.business_system_role_id = role_menu_relation.business_system_role_id ");
                WHERE(" business_system_menu_button.deleted = 0 and business_system_menu.deleted = 0 and role_menu_relation.deleted = 0 and role_user_relation.deleted = 0 ");
                WHERE(" business_system_menu_button.disable_state = 0 and business_system_menu.disable_state = 0 ");
                WHERE(" business_system_id = #{systemId} and role_user_relation.system_user_id = #{userId} ");
                ORDER_BY(" business_system_menu_button.sort ");
            }
        }.toString();
    }

    /**
     * 按照权限获取系统菜单主键集合
     *
     * @param systemId 系统主键
     * @param userId   帐号主键
     * @param menus    菜单路由集合
     * @return
     * @author LiCongLu
     * @date 2020-04-02 14:26
     */
    public String listAdminMenuListByMenus(@Param("systemId") Integer systemId, @Param("userId") Integer userId, @Param("menus") ArrayList<String> menus) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" distinct business_system_menu.id,business_system_menu.parent_id,menu_name,menu_route,menu_redirect_route,menu_icon,sort ");
                SELECT(builder.toString());
                FROM(" business_system_menu ");
                JOIN(" role_menu_relation on role_menu_relation.business_system_menu_id = business_system_menu.id ");
                JOIN(" role_user_relation on role_user_relation.business_system_role_id = role_menu_relation.business_system_role_id ");
                WHERE(" business_system_menu.deleted = 0 and role_menu_relation.deleted = 0 and role_user_relation.deleted = 0 ");
                WHERE(" business_system_menu.disable_state = 0 ");
                WHERE(" business_system_id = #{systemId} and role_user_relation.system_user_id = #{userId} ");

                // 菜单路由集合
                if (valid(menus)) {
                    String menuString = StringUtils.arrayToDelimitedString(menus.toArray(new String[]{}), "','");
                    WHERE(" business_system_menu.menu_route in ('" + menuString + "')");
                }
                GROUP_BY(" business_system_menu.id ");
            }
        }.toString();
    }

    public String listAdminMenuButtonListByMenus(@Param("systemId") Integer systemId, @Param("userId") Integer userId, @Param("menus") ArrayList<String> menus) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" distinct business_system_menu_button.id,business_system_menu.id as menu_id,menu_route,button_name,button_describe ");
                SELECT(builder.toString());
                FROM(" role_menu_button_relation ");
                JOIN(" business_system_menu_button on business_system_menu_button.id = role_menu_button_relation.business_system_menu_button_id " +
                        " and business_system_menu_button.business_system_menu_id = role_menu_button_relation.business_system_menu_id");
                JOIN(" business_system_menu on business_system_menu.id = role_menu_button_relation.business_system_menu_id ");
                JOIN(" role_menu_relation on role_menu_relation.business_system_menu_id =  role_menu_button_relation.business_system_menu_id " +
                        "  and role_menu_relation.business_system_role_id = role_menu_button_relation.business_system_role_id");
                JOIN(" role_user_relation on role_user_relation.business_system_role_id = role_menu_relation.business_system_role_id ");
                WHERE(" role_menu_button_relation.deleted = 0 ");
                WHERE(" business_system_menu_button.deleted = 0 and business_system_menu.deleted = 0 ");
                WHERE(" role_menu_relation.deleted = 0 and role_user_relation.deleted = 0 ");
                WHERE(" business_system_menu_button.disable_state = 0 and business_system_menu.disable_state = 0 ");
                WHERE(" business_system_id = #{systemId} and role_user_relation.system_user_id = #{userId} ");

                // 菜单路由集合
                if (valid(menus)) {
                    String menuString = StringUtils.arrayToDelimitedString(menus.toArray(new String[]{}), "','");
                    WHERE(" business_system_menu.menu_route in ('" + menuString + "')");
                }
                ORDER_BY(" business_system_menu_button.sort ");
            }
        }.toString();
    }
}
